function check()
{
    var username = $("#chosenUsername").val();
    if (username != "") {
        $.ajax({
            type: "GET",
            url:  "CheckAvailability",
            data: {
                target: username
            },
            success: function(response) {
                var status = $(response).find('status').text();
                if (status == 'failed') $("#availability").html("<span style='color:#F00;'>This username is not available!</span>");
                else $("#availability").html("<span style='color:#00ff00;'>This username is available!</span>");
            },
            dataType: "xml"
        });
    }
}

function validate_form ()
{
    valid = true;
    var today = new Date();
    var day   = $("#day").val();
    var month = $("#month").val();
    var year  = $("#year").val();

    if ( $("#NRIC").val() == "" ) {
        alert ( "Please fill in the 'IC' box." );
        valid = false;
        focusIt("NRIC");

    } else if ( $("#firstName").val() == "" || $("#firstName").val() == "First Name") {
        alert ( "Please fill in the 'First name' box." );
        valid = false;
        focusIt("firstName");

    } else if ( $("#lastName").val() ==  "" || $("#lastName").val() ==  "Last Name") {
        alert ( "Please fill in the 'Last name' box." );
        valid = false;
        focusIt("lastName");

    } else if ( $('input:radio[name=sex]:checked').val() == null ) {
        alert ( "Please choose your Gender: Male or Female" );
        valid = false;
        focusIt("sex");

    } else if (  day == '' || day   == "Day" || month == "" || year  == '' || year  == "Year" ) {
        alert ( "Please specify your Date of Birth" );
        valid = false;
        focusIt("day");

    } else if ( parseInt(year, 10) >= today.getFullYear() || parseInt(year, 10) <= today.getFullYear() - 80 || parseInt(day, 10) > 31) {
        alert ( "Please give a valid Date of Birth" );
        valid = false;
        document.register_form.year.value = '';
        focusIt("day");

    } else if ( (month == "3" || month == "5" || month == "8" || month == "10") && parseInt(day, 10) > 30 ) {
        alert ( "Please give a valid Date of Birth" );
        valid = false;
        document.register_form.year.value = '';
        focusIt("day");

    } else if ( month == "1" && ( parseInt(day, 10) > 29 || ( parseInt(day,10) == 29 && ( parseInt(year, 10)%4 > 0 || ( parseInt(year, 10)%4 == 0 && parseInt(year, 10)%100 == 0 ) ) ) ) ) {
        alert ( "Please give a valid Date of Birth" );
        valid = false;
        document.register_form.year.value = '';
        focusIt("day");

    } else if ( $("#countryCode").val() ==  "" ) {
       alert ( "Please fill in the 'Phone Country code' box." );
        valid = false;
        focusIt("countryCode");

    } else if ( $("#phone").val() == "" ) {
        alert ( "Please fill in the 'Phone number' box." );
        valid = false;
        focusIt("phone");

    } else if ( $("#email1").val() == "" ) {
        alert ( "Please fill in the 'Email address' box." );
        valid = false;
        focusIt("email1");

    } else if ( !($("#email1").val().indexOf(".") > 2 && $("#email1").val().indexOf("@") > 0 && $("#email1").val().indexOf(",") < 0) ) {
        alert ( "Please give a valid email address." );
        valid = false;
        focusIt("email1");

    } else if ( $("#email1").val() != $("#email2").val() ) {
        alert ( "The two emails do not match!" );
        valid = false;
        focusIt("email1");

    } else if ( $("#country").val() == "" ) {
        alert ( "Please select your 'Country'." );
        valid = false;
        focusIt("country");

    } else if ( $("#city").val() == "" ) {
        alert ( "Please fill in the 'City' box." );
        valid = false;
        focusIt("city");

    } else if ( $("#chosenUsername").val() == "" ) {
        alert($("#chosenUsername").val());
        alert ( "Please fill in the 'Username' box." );
        valid = false;
        focusIt("chosenUsername");

    } else if ( $("#chosenUsername").val().length < 8 ) {
        alert ( "Please give a Username with at least 8 characters." );
        valid = false;
        focusIt("chosenUsername");

    } else if ( $("#password1").val() == "" ) {
        alert ( "Please fill in the 'Password' box." );
        valid = false; 
        focusIt("password1");

    } else if ( $("#password1").val().length < 8 ) {
        alert ( "Please give a Password with at least 8 characters." );
        valid = false;
        focusIt("password1");

    } else if ( $("#password1").val() != $("#password2").val() ) {
        alert ( "The two passwords do not match!" );
        valid = false;
        focusIt("password1");

    } else if ( $("#terms").prop("checked") == false ) {
        alert ( "Please check the Terms & Conditions box." );
        valid = false;
        focusIt("terms");

    } else {

        var specialChars = "!@#$%^&*()+=-[]\';,./{}|\":<>?";
        var i;

        for (i = 0; i < $("#chosenUsername").val().length; i++) {
            if (specialChars.indexOf($("#chosenUsername").val().charAt(i)) != -1) {
                alert ( "Please give a Username with no special characters." )
                valid = false;
                focusIt("chosenUsername");
                break;
            }
        }

        for (i = 0; i < $("#password1").val().length; i++) {
            if (specialChars.indexOf($("#password1").val().charAt(i)) != -1) {
                alert ( "Please give a Password with no special characters." )
                valid = false;
                focusIt("password1");
                break;
            }
        }

        var numChars = "1234567890";

        for (i = 0; i < day.length; i++) {
            if (numChars.indexOf(day.charAt(i)) < 0) {
                alert ( "Please give a valid Day of Birth." )
                valid = false;
                $("#year").val() = '';
                focusIt("day");
                break;
            }
        }

        for (i = 0; i < year.length; i++) {
            if (numChars.indexOf(year.charAt(i)) < 0) {
                alert ( "Please give a valid Year of Birth." )
                valid = false;
                focusIt("year");
                break;
            }
        }

        for (i = 0; i < $("#countryCode").val().length; i++) {
            if (numChars.indexOf($("#countryCode").val().charAt(i)) < 0) {
                alert ( "Please give a valid Country code." )
                valid = false;
                focusIt("countryCode");
                break;
            }
        }

        for (i = 0; i < $("#phone").val().length; i++) {
            if (numChars.indexOf($("#phone").val().charAt(i)) < 0) {
                alert ( "Please give a valid Phone number." )
                valid = false;
                focusIt("phoneNumber");
                break;
            }
        }

    }

    return valid;
}



